package com.winchain.chainbill.bill.biz.discount.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.bill.base.dao.CbDctDao;
import com.winchain.chainbill.bill.base.dao.CbRecDao;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.biz.discount.service.BillDctQueryService;
import com.winchain.chainbill.bill.biz.discount.vo.BillDct;
import com.winchain.chainbill.bill.biz.discount.vo.DctForInvoiceVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecQueryVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("billDctQueryService")
public class BillDctQueryServiceImpl implements BillDctQueryService {
	@Autowired
	CbRecDao cbRecordDao;
	@Autowired
	CbDctDao cbDctDao;

	@Pagination
	public List<CbRecVo> selectBillForDct(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setHldrId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		cbRecordQueryVo.setBillState("S005");

		cbRecordQueryVo.setCurrentDate(SysDateUtil.getSystemDate());

		ParameterVo dctLimtDay = ParameterHelper.getCommonParameter("DCT_LIM_DAY");
		if ((dctLimtDay != null) && (StringUtil.notEmpty(dctLimtDay.getParmValue()))) {
			cbRecordQueryVo.setLimtDay(dctLimtDay.getParmValue());
		}
		List<CbRecVo> bills = this.cbRecordDao.selectOwnBillForDct(cbRecordQueryVo);
		return bills;
	}

	@Pagination
	public List<BillDct> selectBillForDctReviewByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setSellerId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		String[] billStates = { "S013", "S030" };

		cbRecordQueryVo.setBillStates(billStates);
		return this.cbDctDao.selectDcts(cbRecordQueryVo);
	}

	public CbDct selectDctByPrntId(String prntId) {
		Example example = new Example(CbDct.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("prntId", prntId);
		criteria.andNotEqualTo("dctState", "0");

		List<CbDct> list = this.cbDctDao.selectByExample(example);
		if ((list != null) && (list.size() > 0)) {
			return (CbDct) list.get(0);
		}
		return null;
	}

	public CbDct selectByBillId(String billId) {
		Example example = new Example(CbDct.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("billId", billId);
		criteria.andNotEqualTo("dctState", "0");

		List<CbDct> list = this.cbDctDao.selectByExample(example);
		if ((list != null) && (list.size() > 0)) {
			return (CbDct) list.get(0);
		}
		return null;
	}

	@Pagination
	public List<BillDct> selectDctBillForPaid(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setFinId(CommonDataUtil.getCurEntId());
		cbRecordQueryVo.setDctDate(SysDateUtil.getSystemDate());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		if (StringUtil.notEmpty(cbRecordQueryVo.getSellerNm())) {
			cbRecordQueryVo.setSellerNm(StringUtil.getParameterLike(cbRecordQueryVo.getSellerNm()));
		}
		return this.cbDctDao.selectDctBillForPaid(cbRecordQueryVo);
	}

	public List<BillDct> selectDctBillForPaid(CbRecQueryVo cbRecordQueryVo, String cusId) {
		cbRecordQueryVo.setFinId(cusId);
		cbRecordQueryVo.setDctDate(SysDateUtil.getSystemDate());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		if (StringUtil.notEmpty(cbRecordQueryVo.getSellerNm())) {
			cbRecordQueryVo.setSellerNm(StringUtil.getParameterLike(cbRecordQueryVo.getSellerNm()));
		}
		return this.cbDctDao.selectDctBillForPaid(cbRecordQueryVo);
	}

	@Pagination
	public List<DctForInvoiceVo> selectUnApplyBills(PageLimit limit, String dctIds, String finId, String invoiceType) {
		String[] idArr = null;
		if (!StringUtil.emptyAndNull(dctIds)) {
			idArr = dctIds.split(",");
		}
		return this.cbDctDao.selectUnApplyBills("3", "2", CommonDataUtil.getCurEntId(), finId, invoiceType,
				Boolean.valueOf("2".equals(invoiceType)), idArr);
	}

	public List<DctForInvoiceVo> selectApplyBills(String invoiceId) {
		return this.cbDctDao.selectApplyBills(invoiceId);
	}

	@Pagination
	public List<BillDct> selectDctBillForPrePaid(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setCurrentDate(SysDateUtil.getSystemDate());
		return selectDctBillForPaid(limit, cbRecordQueryVo);
	}

	public List<BillDct> selectDctBillForPrePaid(CbRecQueryVo cbRecordQueryVo, String cusId) {
		cbRecordQueryVo.setCurrentDate(SysDateUtil.getSystemDate());
		return selectDctBillForPaid(cbRecordQueryVo, cusId);
	}

	@Pagination
	public List<BillDct> selectBillForDctApproveByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setFinId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		cbRecordQueryVo.setDctDate(SysDateUtil.getSystemDate());
		if (StringUtil.notEmpty(cbRecordQueryVo.getSellerNm())) {
			cbRecordQueryVo.setSellerNm(StringUtil.getParameterLike(cbRecordQueryVo.getSellerNm()));
		}
		return this.cbDctDao.selectDctBillForApprove(cbRecordQueryVo);
	}

	@Pagination
	public List<BillDct> selectBillForDctMgtByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setFinId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		cbRecordQueryVo.setDctDate(SysDateUtil.getSystemDate());
		if (StringUtil.notEmpty(cbRecordQueryVo.getSellerNm())) {
			cbRecordQueryVo.setSellerNm(StringUtil.getParameterLike(cbRecordQueryVo.getSellerNm()));
		}
		return this.cbDctDao.selectDctBillForMgtByStates(cbRecordQueryVo);
	}

	@Pagination
	public List<BillDct> selectUnuploadInvoiceListByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		cbRecordQueryVo.setSellerId(CommonDataUtil.getCurEntId());
		if (StringUtil.notEmpty(cbRecordQueryVo.getBillCode())) {
			cbRecordQueryVo.setBillCode(StringUtil.getParameterLike(cbRecordQueryVo.getBillCode()));
		}
		List<BillDct> list = this.cbDctDao.selectUnuploadInvoiceList(cbRecordQueryVo);
		String curDate = SysDateUtil.getSystemDate();
		for (BillDct billDct : list) {
			if (StringUtil.notEmpty(billDct.getDfrUploadExpDate())) {
				int days = SysDateUtil.getDaysBetween(curDate, billDct.getDfrUploadExpDate());
				billDct.setRemainInvDfrDays(days);
			}
		}
		return list;
	}
}
